package com.iwhalecloud.bss.kite.cucc.order.query;

import com.iwhalecloud.bss.kite.cucc.order.util.InstProxyUtils;
import com.iwhalecloud.bss.kite.dataservice.entity.CustomerOrder;
import com.iwhalecloud.bss.kite.dataservice.entity.OrderAttr;
import com.iwhalecloud.bss.kite.dataservice.entity.OrderItem;
import com.iwhalecloud.bss.kite.dataservice.entity.OrderItemAttr;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.IInst;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.OfferInst;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.OfferInstAssureExt;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.OfferInstAttr;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.OfferObjInstRel;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.OfferProdInstRel;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.OfferResInstRelExt;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.ProdInst;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.ProdInstAcctRel;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.ProdInstFunc;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.ProdInstFuncExt;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.ProdInstRel;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.ProdInstState;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.SubProdInst;
import com.iwhalecloud.bss.kite.order.client.api.query.IQueryOrderInstService;
import com.iwhalecloud.bss.kite.order.client.context.OrderContext;
import com.iwhalecloud.bss.kite.order.client.dto.InstContainer;
import com.iwhalecloud.bss.kite.order.client.dto.OrderContainer;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class QueryOrderInstService implements IQueryOrderInstService {

    @Override
    public <T extends IInst> T getInst(String tableName, String instId) {
        T inst = InstContainer.ofRequest(OrderContext.getOrderRequest()).getInst(tableName, instId);
        return InstProxyUtils.createProxy(inst);
    }

    @Override
    public OfferInst getOfferInst(String offerInstId) {
        OfferInst offerInst = InstContainer.ofRequest(OrderContext.getOrderRequest()).getOfferInst(offerInstId);
        return InstProxyUtils.createProxy(offerInst);
    }

    @Override
    public List<OfferInst> listOfferInsts() {
        List<OfferInst> offerInsts = InstContainer.ofRequest(OrderContext.getOrderRequest()).listOfferInsts();
        return InstProxyUtils.createProxy(offerInsts);
    }

    @Override
    public List<OfferInstAttr> listOfferInstAttrs(String offerInstId) {
        List<OfferInstAttr> offerInstAttrs = InstContainer.ofRequest(OrderContext.getOrderRequest())
            .listOfferInstAttrs(offerInstId);
        return InstProxyUtils.createProxy(offerInstAttrs);
    }

    @Override
    public ProdInst getProdInst(String prodInstId) {
        ProdInst prodInst = InstContainer.ofRequest(OrderContext.getOrderRequest()).getProdInst(prodInstId);
        return InstProxyUtils.createProxy(prodInst);
    }

    @Override
    public List<ProdInst> listProdInsts() {
        List<ProdInst> prodInsts = InstContainer.ofRequest(OrderContext.getOrderRequest()).listProdInsts();
        return InstProxyUtils.createProxy(prodInsts);
    }

    @Override
    public OfferObjInstRel getOfferObjInstRel(String offerObjInstRelId) {
        OfferObjInstRel offerObjInstRel = InstContainer.ofRequest(OrderContext.getOrderRequest())
            .getOfferObjInstRel(offerObjInstRelId);
        return InstProxyUtils.createProxy(offerObjInstRel);
    }

    @Override
    public List<OfferObjInstRel> listOfferObjInstRels() {
        List<OfferObjInstRel> offerObjInstRels = InstContainer.ofRequest(OrderContext.getOrderRequest())
            .listOfferObjInstRels();
        return InstProxyUtils.createProxy(offerObjInstRels);
    }

    @Override
    public OfferProdInstRel getOfferProdInstRel(String offerProdInstRelId) {
        OfferProdInstRel offerProdInstRel = InstContainer.ofRequest(OrderContext.getOrderRequest())
            .getOfferProdInstRel(offerProdInstRelId);
        return InstProxyUtils.createProxy(offerProdInstRel);
    }

    @Override
    public List<OfferProdInstRel> listOfferProdInstRels() {
        List<OfferProdInstRel> offerProdInstRels = InstContainer.ofRequest(OrderContext.getOrderRequest())
            .listOfferProdInstRels();
        return InstProxyUtils.createProxy(offerProdInstRels);
    }

    @Override
    public OfferResInstRelExt getOfferResInstRelExt(String offerResInstRelId) {
        OfferResInstRelExt offerResInstRelExt = InstContainer.ofRequest(OrderContext.getOrderRequest())
            .getOfferResInstRelExt(offerResInstRelId);
        return InstProxyUtils.createProxy(offerResInstRelExt);
    }

    @Override
    public List<OfferResInstRelExt> listOfferResInstRelExts() {
        List<OfferResInstRelExt> offerResInstRelExts = InstContainer.ofRequest(OrderContext.getOrderRequest())
            .listOfferResInstRelExts();
        return InstProxyUtils.createProxy(offerResInstRelExts);
    }

    @Override
    public ProdInstRel getProdInstRel(String prodInstRelId) {
        ProdInstRel prodInstRel = InstContainer.ofRequest(OrderContext.getOrderRequest()).getProdInstRel(prodInstRelId);
        return InstProxyUtils.createProxy(prodInstRel);
    }

    @Override
    public List<ProdInstRel> listProdInstRels() {
        List<ProdInstRel> prodInstRels = InstContainer.ofRequest(OrderContext.getOrderRequest()).listProdInstRels();
        return InstProxyUtils.createProxy(prodInstRels);
    }

    @Override
    public ProdInstFunc getProdInstFunc(String prodInstId) {
        ProdInstFunc prodInstFunc = InstContainer.ofRequest(OrderContext.getOrderRequest()).getProdInstFunc(prodInstId);
        return InstProxyUtils.createProxy(prodInstFunc);
    }

    @Override
    public List<ProdInstFunc> listProdInstFuncs(String prodInstId) {
        List<ProdInstFunc> prodInstFuncs = InstContainer.ofRequest(OrderContext.getOrderRequest())
            .listProdInstFuncs(prodInstId);
        return InstProxyUtils.createProxy(prodInstFuncs);
    }

    @Override
    public List<ProdInstFuncExt> listProdInstFuncExts(String prodInstId) {
        List<ProdInstFuncExt> prodInstFuncExts = InstContainer.ofRequest(OrderContext.getOrderRequest())
            .listProdInstFuncExts(prodInstId);
        return InstProxyUtils.createProxy(prodInstFuncExts);
    }

    @Override
    public ProdInstAcctRel getProdInstAcctRel(String prodInstAcctRelId) {
        ProdInstAcctRel prodInstAcctRel = InstContainer.ofRequest(OrderContext.getOrderRequest())
            .getProdInstAcctRel(prodInstAcctRelId);
        return InstProxyUtils.createProxy(prodInstAcctRel);
    }

    @Override
    public List<ProdInstAcctRel> listProdInstAcctRels(String prodInstId) {
        List<ProdInstAcctRel> prodInstAcctRels = InstContainer.ofRequest(OrderContext.getOrderRequest())
            .listProdInstAcctRels(prodInstId);
        return InstProxyUtils.createProxy(prodInstAcctRels);
    }

    @Override
    public ProdInstState getProdInstState(String prodInstStateId) {
        ProdInstState prodInstState = InstContainer.ofRequest(OrderContext.getOrderRequest())
            .getProdInstState(prodInstStateId);
        return InstProxyUtils.createProxy(prodInstState);
    }

    @Override
    public List<ProdInstState> listProdInstStates(String prodInstId) {
        List<ProdInstState> prodInstStates = InstContainer.ofRequest(OrderContext.getOrderRequest())
            .listProdInstStates();
        return InstProxyUtils.createProxy(prodInstStates);
    }

    @Override
    public OfferInstAssureExt getOfferInstAssureExt(String offerInstAssureExtId) {
        OfferInstAssureExt offerInstAssureExt = InstContainer.ofRequest(OrderContext.getOrderRequest())
            .getOfferInstAssureExt(offerInstAssureExtId);
        return InstProxyUtils.createProxy(offerInstAssureExt);
    }

    @Override
    public List<OfferInstAssureExt> listOfferInstAssureExts(String offerInstId) {
        List<OfferInstAssureExt> offerInstAssureExts = InstContainer.ofRequest(OrderContext.getOrderRequest())
            .listOfferInstAssureExts(offerInstId);
        return InstProxyUtils.createProxy(offerInstAssureExts);
    }

    @Override
    public SubProdInst getSubProdInst(String prodInstId) {
        SubProdInst subProdInst = InstContainer.ofRequest(OrderContext.getOrderRequest()).getSubProdInst(prodInstId);
        return InstProxyUtils.createProxy(subProdInst);
    }

    @Override
    public List<SubProdInst> listSubProdInsts(String offerInstId) {
        List<SubProdInst> subProdInsts = InstContainer.ofRequest(OrderContext.getOrderRequest())
            .listSubProdInsts(offerInstId);
        return InstProxyUtils.createProxy(subProdInsts);
    }

    @Override
    public CustomerOrder getCustomerOrder() {
        return OrderContext.getOrderRequest().getOrderContainer().getCustomerOrder();
    }

    @Override
    public List<OrderAttr> listOrderAttrs(String custOrderId) {
        return OrderContext.getOrderRequest().getOrderContainer().getOrderAttrMap().getOrDefault(custOrderId,
            new ArrayList<>());
    }

    @Override
    public OrderItem getOrderItem(String orderItemId) {
        return OrderContainer.ofRequest(OrderContext.getOrderRequest()).getOrderItem(orderItemId);
    }

    @Override
    public List<OrderItem> listOrderItems() {
        return OrderContainer.ofRequest(OrderContext.getOrderRequest()).listOrderItems();
    }

    @Override
    public List<OrderItemAttr> listOrderItemAttrs(String orderItemId) {
        return OrderContainer.ofRequest(OrderContext.getOrderRequest()).listOrderItemAttrs(orderItemId);
    }
}
